package pacBioSimple;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pacBiopackage.GetOpt;
import tools.sequences.sequenceUtils;

public class FindExactDuplicateSeqMain 
{
	public static void main(String[] args) {
		GetOpt go = new GetOpt(args, "o:k:a:h:n:l:e:j");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
		String filenameInput = "";   
		String filenameOutput="output.txt";
		
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		    }
		    else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else if((char)ch =='a')
		    {
		    	filenameInput=go.optArgGet();
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenameInput.isEmpty()) {
		    System.out.println("Usage: -a input [ -o outputFile -h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-o " + filenameOutput); //NOI18N
		System.out.println("-a " + filenameInput); //NOI18N	
		System.out.println("------------------------");
		
		try
		{
			BufferedReader in = null;
			
			in = new BufferedReader(new FileReader(filenameInput));
			String header,sequence;
			Map<String, List<String> > collision = new HashMap<String, List<String>>();
			while(in.ready())
			{
				header=in.readLine(); //header;
				sequence=in.readLine();

				if(collision.size()==0)
				{
					collision.put(sequence, new ArrayList<String>());
					collision.get(sequence).add(header);
				}
				else
				{
					//now check for collision;
					if(collision.containsKey(sequence))
					{
						collision.get(sequence).add(header);
					}
					else
					{
						String b = sequenceUtils.reverseComplement(sequence);
						if(collision.containsKey(b))
						{
							collision.get(b).add(header);
						}
						else
						{
							collision.put(sequence, new ArrayList<String>());
							collision.get(sequence).add(header);
						}
					}
				}
			}
			//now we have to print out the map
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_unique")));
			PrintWriter pw2 = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_not_unique")));
			PrintWriter pw3 = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_stat")));
			for(Map.Entry<String, List<String> >entry : collision.entrySet())
			{
				List<String> numberOfDuplication =entry.getValue(); 
				pw3.append(String.valueOf(numberOfDuplication.size()+"\n"));
				if(numberOfDuplication.size()==1)
				{
					pw.append(numberOfDuplication.get(0)+"\n");
					pw.append(entry.getKey()+"\n");
					
				}
				else
				{
					StringBuilder sb = new StringBuilder();
					for(String name :numberOfDuplication)
					{
						sb.append(name+" ");
					}
					pw2.append(sb.toString()+"\n");
					pw2.append(entry.getKey()+"\n");
				}			
			}
			pw.flush();
			pw2.flush();
			pw3.flush();
			pw.close();
			pw2.close();
			pw3.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
}
